package com.qut.graduate.busrtquery.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qut.graduate.busrtquery.R;
import com.qut.graduate.busrtquery.bean.BusInfoLineQuery;
import com.qut.graduate.busrtquery.greendao.bean.BusLine;
import com.qut.graduate.busrtquery.greendao.bean.BusLineCollect;
import com.qut.graduate.busrtquery.interfaces.OnItemClickListener;

import java.util.List;

public class BusLineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "BusLineAdapter";
    private Context mContext;
    private List<?> mBusLineList;
    private int tag;

    public BusLineAdapter(Context context, List<?> mBusLineList, int tag) {
        this.mContext = context;
        this.tag = tag;
        this.mBusLineList = mBusLineList;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return mBusLineList != null ? mBusLineList.size() : 0;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_line_query, parent, false);
        return new BusLineHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    mOnItemClickListener.onClick(holder.itemView, position);
                }
            });
        }
        BusLineHolder busLineHolder = (BusLineHolder) holder;
        BusLine busLine = null;
        if (tag == 0){
            busLine = (BusLine) mBusLineList.get(position);
        }if (tag == 1){
            busLine = new BusLine((BusLineCollect)mBusLineList.get(position));
        }
        busLineHolder.mBusName.setText(busLine.getName());
        busLineHolder.mBusLine.setText(busLine.getLine());
        if (getItemCount() - 1 > 0 && position == getItemCount() - 1)
            busLineHolder.mBottomLine.setVisibility(View.GONE);
        else
            busLineHolder.mBottomLine.setVisibility(View.VISIBLE);
    }

    public class BusLineHolder extends RecyclerView.ViewHolder {
        TextView mBusName;
        TextView mBusLine;
        View mBottomLine;

        public BusLineHolder(View view) {
            super(view);
            mBusName = view.findViewById(R.id.item_line_query_bus_name_text);
            mBusLine = view.findViewById(R.id.item_line_query_bus_line_text);
            mBottomLine = view.findViewById(R.id.item_line_query_bottom_line);
        }
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener OnItemClickListener) {
        this.mOnItemClickListener = OnItemClickListener;
    }

}
